package com.system.runtai.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.system.common.result.Result;
import com.system.runtai.entity.*;
import com.system.runtai.entity.VO.CompanyAnnouncementsDto;
import com.system.runtai.service.*;
import com.system.runtai.utils.AuthContextUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.bouncycastle.asn1.cms.PasswordRecipientInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 润泰公告 前端控制器
 * </p>
 *
 * @author runtai
 * @since 2024-03-29
 */
@Api(tags = "润泰公告")
@RestController
@RequestMapping("/runtai/company-announcements")
public class CompanyAnnouncementsController {

    @Autowired
    private ArticleCollectService articleCollectService;
    @Autowired
    private CompanyAnnouncementsService companyAnnouncementsService;

    @Autowired
    private SecondaryCopywriterService secondaryCopywriterService;

    @Autowired
    private InRegardOfService inRegardOfService;

    @Autowired
    private CompanyIntroductionService companyIntroductionService;
    @Autowired
    private CompanyDynamicService companyDynamicService;

    @Autowired
    private CompanyCaseService companyCaseService;



    @ApiOperation(value = "润泰公告二级文案")
    @GetMapping("/getSecondaryCopywriter/{id}")
    public Result<List<SecondaryCopywriter>> getSecondaryCopywriter(@PathVariable Integer id,@RequestParam(required = false) String title){
        if (StringUtils.isEmpty(title)) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 5).eq(SecondaryCopywriter::getMainArgumentId, id);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }else if (!StringUtils.isEmpty(title)){
            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
            wrapper.eq(SecondaryCopywriter::getStartid, 5).eq(SecondaryCopywriter::getMainArgumentId, id).like(SecondaryCopywriter::getTitle,title);
            List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
            return Result.ok(list);
        }

        List<SecondaryCopywriter> list = null;
        return Result.fail(list).message("对象为空");
    }


//    @ApiOperation(value = "润泰公告二级文案标题筛选")
//    @GetMapping("/getSecondaryCopywriterfilter")
//    public Result<List<SecondaryCopywriter>> getSecondaryCopywriterfilter(@RequestParam(required = false) String title){
//        LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//        wrapper.like(SecondaryCopywriter::getTitle,title).eq();
//        List<SecondaryCopywriter> list = secondaryCopywriterService.list(wrapper);
//
//        return Result.ok(list);
//    }


    @ApiOperation(value = "添加润泰公告二级文案")
    @PostMapping("/saveSecondaryCopywriter/{id}")
    public Result<SecondaryCopywriter> saveSecondaryCopywriter(@PathVariable Integer id,@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setCreateTime(LocalDateTime.now());
        secondaryCopywriter.setStartid(5);
        secondaryCopywriter.setMainArgumentId(id);
        boolean save = secondaryCopywriterService.save(secondaryCopywriter);

        if (save){
            return Result.ok(secondaryCopywriter).message("添加成功");
        }
        return Result.fail(secondaryCopywriter).message("添加失败");
    }


    @ApiOperation(value = "修改二级文案")
    @PostMapping("/updateSecondaryCopywriter")
    public Result<SecondaryCopywriter> updateSecondaryCopywriter(@RequestBody SecondaryCopywriter secondaryCopywriter){

        secondaryCopywriter.setUpdateTime(LocalDateTime.now());

        boolean b = secondaryCopywriterService.updateById(secondaryCopywriter);

        if (b){
            return Result.ok(secondaryCopywriter).message("修改成功");
        }
        return Result.fail(secondaryCopywriter).message("修改失败");
    }

    @ApiOperation(value = "删除二级文案")
    @PostMapping("delectSecondaryCopywriter/{id}")
    public Result delectSecondaryCopywriter(@PathVariable Integer id){

        boolean b = secondaryCopywriterService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }


    @ApiOperation(value = "所有润泰公告")
    @GetMapping("/companyAnnouncementsList")
    public Result<List<CompanyAnnouncements>> companyAnnouncementsList(){
        List<CompanyAnnouncements> list = companyAnnouncementsService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "后台润泰公告详情")
    @GetMapping("/companyAnnouncements/{id}")
    public Result<CompanyAnnouncements> companyAnnouncementsById(@PathVariable Integer id){
        CompanyAnnouncements byId = companyAnnouncementsService.getById(id);
        return Result.ok(byId);
    }


    @ApiOperation(value = "程序润泰公告详情")
    @GetMapping("/companyAnnouncementsCollect/{id}")
    public Result<CompanyAnnouncements> CollectByIdcx(@PathVariable Integer id) {
        CompanyAnnouncements byId = companyAnnouncementsService.getById(id);

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId, 5).eq(ArticleCollect::getInRegardOfId, id);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one == null) {
            byId.setCollect(0);
            return Result.ok(byId);
        } else {
            byId.setCollect(1);
            return Result.ok(byId);
        }
    }


    @ApiOperation(value = "查看所有润泰公告一、二级文案")
    @GetMapping("/getCompanyAnnouncementsSC/{startId}")
    public Result<IPage<CompanyAnnouncements>> getCompanyAnnouncementsList(@PathVariable Integer startId,@RequestParam(required = false) Integer page, @RequestParam(required = false) Integer limit){
        List<CompanyAnnouncements> list = companyAnnouncementsService.list();
//        for (int i = 0; i < list.size(); i++) {
//            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,list.get(i).getId());
//            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper);
//            list.get(i).setSecondaryCopywriterList(copywriterList);
//        }
//        return Result.ok(list);

        Page<CompanyAnnouncements> recommendPage = new Page<>(page,limit);
        LambdaQueryWrapper<CompanyAnnouncements> wrapper = new LambdaQueryWrapper<>();
        Integer id = null;
        for (CompanyAnnouncements regardOf : list) {
            id = regardOf.getId();
        }
        if (id!=null){
            wrapper.eq(CompanyAnnouncements::getId,id).orderByDesc(CompanyAnnouncements::getCreateTime);
        }
        IPage<CompanyAnnouncements> iPage = companyAnnouncementsService.page(recommendPage,wrapper);
        for (CompanyAnnouncements record : iPage.getRecords()) {
            LambdaQueryWrapper<SecondaryCopywriter> wrapper1 = new LambdaQueryWrapper<>();
            wrapper1.eq(SecondaryCopywriter::getStartid,startId).eq(SecondaryCopywriter::getMainArgumentId,record.getId());
            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper1);
            record.setSecondaryCopywriterList(copywriterList);
        }
        return Result.ok(iPage);

    }

//    @ApiOperation(value = "查看所有润泰公告一、二级文案")
//    @GetMapping("/getCompanyAnnouncementsSC")
//    public Result<List<CompanyAnnouncements>> getCompanyAnnouncements(){
//        List<CompanyAnnouncements> list = companyAnnouncementsService.list();
//        for (int i = 0; i < list.size(); i++) {
//            LambdaQueryWrapper<SecondaryCopywriter> wrapper = new LambdaQueryWrapper<>();
//            wrapper.eq(SecondaryCopywriter::getStartid,list.get(i).getId()).eq(SecondaryCopywriter::getMainArgumentId,list.get(i).getId());
//            List<SecondaryCopywriter> copywriterList = secondaryCopywriterService.list(wrapper);
//            list.get(i).setSecondaryCopywriterList(copywriterList);
//        }
//        return Result.ok(list);
//    }

    @ApiOperation(value = "添加公告")
    @PostMapping("/saveCompanyAnnouncements")
    public Result saveCompanyAnnouncements(@RequestBody CompanyAnnouncementsDto companyAnnouncements){

        if (companyAnnouncements.getStart()==1){
            InRegardOf inRegardOf = new InRegardOf();
            inRegardOf.setTitle(companyAnnouncements.getTitle());
            inRegardOf.setArticle(companyAnnouncements.getArticle());
            inRegardOf.setPicture(companyAnnouncements.getPicture());
//            inRegardOf.setVideo(companyAnnouncements.getVideo());
            inRegardOf.setStart(1);
            inRegardOf.setCreateTime(LocalDateTime.now());
            boolean save = inRegardOfService.save(inRegardOf);

            if (save){
                return Result.ok(inRegardOf).message("添加成功");
            }
            return Result.fail(inRegardOf).message("添加失败");
        }else if (companyAnnouncements.getStart()==2){

            CompanyIntroduction companyIntroduction = new CompanyIntroduction();
            companyIntroduction.setTitle(companyAnnouncements.getTitle());
            companyIntroduction.setArticle(companyAnnouncements.getArticle());
            companyIntroduction.setPicture(companyAnnouncements.getPicture());
//            companyIntroduction.setVideo(companyAnnouncements.getVideo());
            companyIntroduction.setStart(2);
            companyIntroduction.setCreateTime(LocalDateTime.now());
            boolean save = companyIntroductionService.save(companyIntroduction);

            if (save){
                return Result.ok(companyIntroduction).message("添加成功");
            }else {
                return Result.fail(companyIntroduction).message("添加失败");
            }
        }else if (companyAnnouncements.getStart()==3){

            CompanyDynamic companyDynamic = new CompanyDynamic();
            companyDynamic.setTitle(companyAnnouncements.getTitle());
            companyDynamic.setArticle(companyAnnouncements.getArticle());
            companyDynamic.setPicture(companyAnnouncements.getPicture());
//            companyDynamic.setVideo(companyAnnouncements.getVideo());
            companyDynamic.setStart(3);
            companyDynamic.setCreateTime(LocalDateTime.now());
            boolean save = companyDynamicService.save(companyDynamic);

            if (save){
                return Result.ok(companyDynamic).message("添加成功");
            }else {
                return Result.fail(companyDynamic).message("添加失败");
            }

        }else if (companyAnnouncements.getStart()==4){

            CompanyCase companyCase = new CompanyCase();
            companyCase.setTitle(companyAnnouncements.getTitle());
            companyCase.setArticle(companyAnnouncements.getArticle());
            companyCase.setPicture(companyAnnouncements.getPicture());
//            companyCase.setVideo(companyAnnouncements.getVideo());
            companyCase.setStart(4);
            companyCase.setCreateTime(LocalDateTime.now());
            boolean save = companyCaseService.save(companyCase);

            if (save) {
                return Result.ok(companyCase).message("添加成功");
            } else {
                return Result.fail(companyCase).message("添加失败");
            }

        }else if (companyAnnouncements.getStart()==5){
            CompanyAnnouncements announcements = new CompanyAnnouncements();
            announcements.setTitle(companyAnnouncements.getTitle());
            announcements.setArticle(companyAnnouncements.getArticle());
            announcements.setPicture(companyAnnouncements.getPicture());
//            announcements.setVideo(companyAnnouncements.getVideo());
            announcements.setStart(5);
            announcements.setCreateTime(LocalDateTime.now());
            boolean save = companyAnnouncementsService.save(announcements);
            if (save){
                return Result.ok(companyAnnouncements).message("添加成功");
            }else {
                return Result.fail(companyAnnouncements).message("添加失败");
            }

        }

        return Result.fail().message("没有该类型参数！");
    }


    @ApiOperation(value = "修改润泰公告")
    @PostMapping("/updateCompanyAnnouncements")
    public Result<CompanyAnnouncements> updateCompanyAnnouncements(@RequestBody CompanyAnnouncements companyAnnouncements){

        companyAnnouncements.setUpdateTime(LocalDateTime.now());
        boolean update = companyAnnouncementsService.updateById(companyAnnouncements);

        if (update){
            return Result.ok(companyAnnouncements).message("修改成功");
        }else {
            return Result.ok(companyAnnouncements).message("修改失败");
        }
    }


    @ApiOperation(value = "删除润泰公告")
    @PostMapping("delectCompanyAnnouncements/{id}")
    public Result delectCompanyAnnouncements(@PathVariable Integer id){

        boolean b = companyAnnouncementsService.removeById(id);

        if (b){
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

    @ApiOperation(value = "分页查询润泰公告")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<CompanyAnnouncements>> index54(@PathVariable Long page, @PathVariable Long limit, CompanyAnnouncements companyAnnouncements){

        Page<CompanyAnnouncements> recommendPage = new Page<>(page,limit);

        LambdaQueryWrapper<CompanyAnnouncements> wrapper = new LambdaQueryWrapper<>();

        Integer id = companyAnnouncements.getId();

        if (id!=null){
            wrapper.eq(CompanyAnnouncements::getId,id);
        }

        IPage<CompanyAnnouncements> iPage = companyAnnouncementsService.page(recommendPage,wrapper);

        return Result.ok(iPage);
    }


    @ApiOperation(value = "收藏润泰公告：5")
    @PostMapping("collectCompanyAnnouncements")
    public Result collectCompanyAnnouncements(@RequestBody CompanyAnnouncements companyCase) {

        String s = AuthContextUtil.get();
        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyCase.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,5);
        ArticleCollect one = articleCollectService.getOne(wrapper);

        if (one!=null){
            return Result.ok(one);
        }else {
            ArticleCollect articleCollect = new ArticleCollect();
            articleCollect.setCreateTime(LocalDateTime.now());
            articleCollect.setAppletUserId(Integer.valueOf(s));
            articleCollect.setInRegardOfId(companyCase.getId());
            articleCollect.setStartId(5);
            boolean b = articleCollectService.save(articleCollect);

            if (b) {
                return Result.ok().message("添加收藏成功");
            }
            return Result.fail().message("添加收藏失败");
        }
    }

    @ApiOperation(value = "删除润泰公告：5收藏")
    @PostMapping("delectCompanyCaseCollect")
    public Result delectCompanyCaseCollect(@RequestBody CompanyDynamic companyDynamic) {
        String s = AuthContextUtil.get();

        LambdaQueryWrapper<ArticleCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ArticleCollect::getInRegardOfId, companyDynamic.getId()).eq(ArticleCollect::getAppletUserId, Integer.valueOf(s)).eq(ArticleCollect::getStartId,5);
        boolean b = articleCollectService.removeById(wrapper);

        if (b) {
            return Result.ok().message("删除成功");
        }
        return Result.fail().message("删除失败");
    }

}

